﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.AxHost;

namespace AnimEditor
{
    public partial class Main : Form
    {
        private string current_path = "";

        private void SetTile()
        {
            this.Text = "AnimEditor - " + current_path;
        }
        public Main()
        {
            InitializeComponent();
        }

        private void Main_Load(object sender, EventArgs e)
        {
            
        }

        private void 菜单_文件_打开_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                string path = folderBrowserDialog.SelectedPath;
                if(path != "")
                {
                    this.current_path = path;
                    SetTile();
                    //清空列表
                    目录列表.Items.Clear();
                    图片列表.Items.Clear();
                    DirectoryInfo directoryInfo = new DirectoryInfo(path);
                    var directroies = directoryInfo.GetDirectories();
                    处理进度.Maximum = directroies.Length;
                    处理进度.Value = 0;
                    for(int i = 0; i < directroies.Length; i++)
                    {
                        处理进度.Value = i;
                        目录列表.Items.Add(directroies[i].Name,0);
                        目录数量.Text ="文件数："+ 目录列表.Items.Count.ToString();
                    }
                    处理进度.Value = directroies.Length;
                    目录列表.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);              
                }
            }
        }

        private void 目录列表_Click(object sender, EventArgs e)
        {
            刷新图像();
        }

        private void 目录列表_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            刷新图像();
        }

        private void 图片列表_Click(object sender, EventArgs e)
        {
            刷新显示();
        }

        private void 图片列表_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            刷新显示();
        }

        private void 刷新图像()
        {
            if (current_path != "" && 目录列表.Items.Count > 0 && 目录列表.SelectedItems.Count == 1)
            {
                图片列表.Items.Clear();
                string[] files = Directory.GetFiles(current_path + "\\" + 目录列表.SelectedItems[0].Text, "*.png", SearchOption.AllDirectories);
                处理进度.Maximum = files.Length;
                处理进度.Value = 0;
                for (int i = 0; i < files.Length; i++)
                {
                    处理进度.Value = i;

                    图片列表.Items.Add(files[i].Replace(current_path + "\\" + 目录列表.SelectedItems[0].Text + "\\", ""), 1);
                    图片数量.Text = "图片数：" + 图片列表.Items.Count.ToString();
                }
                处理进度.Value = files.Length;
                图片列表.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
            }
        }

        private void 刷新显示()
        {
            if (current_path != "" && 目录列表.Items.Count > 0 && 目录列表.SelectedItems.Count == 1 && 图片列表.Items.Count > 0 && 图片列表.SelectedItems.Count == 1)
            {
                string img_path = current_path + "\\" + 目录列表.SelectedItems[0].Text + "\\" + 图片列表.SelectedItems[0].Text;
                Image image = Image.FromFile(img_path);
                预览图像.Image = Image.FromFile(img_path);
                预览图像.Left = 预览图像.Top = 0;
                预览图像.Width = image.Width;
                预览图像.Height = image.Height;
            }
        }
    } 
}
